package com.renwei.yunlong.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author ysq
 *         关于已过去时间的帮助类
 */
public class GetTimeUtils {

    //获取 年月日
    public static String getYMDTime(long time) {
        String brith_StrTime = null;
        SimpleDateFormat sdf = null;
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        brith_StrTime = sdf.format(new Date(time));
        return brith_StrTime;
    }

    public static String getYMTime(long time) {
        String brith_StrTime = null;
        SimpleDateFormat sdf = null;
        sdf = new SimpleDateFormat("yyyy-MM");
        brith_StrTime = sdf.format(new Date(time));
        return brith_StrTime;
    }


    /**
     * 根据年月获得 这个月总共有几天
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDay(int year, int month) {
        if (month == 0) {
            month = 12;
        }
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    //获取上个月
    public static String getLastMonth(long time) {
        try {
            DateFormat fmt = new SimpleDateFormat("yyyy-MM");
            String strDate = fmt.format(new Date(time));
            Calendar c = Calendar.getInstance();
            if (!"".equals(strDate)) {
                c.setTime(fmt.parse(strDate));
            }
            c.add(Calendar.MONTH, -1);
            return fmt.format(c.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // 获取上个月的最后一天
    public static String getLastMonthDay(long time) {
        try {
            DateFormat fmt = new SimpleDateFormat("yyyy-MM");
            String strDate = fmt.format(new Date(time));
            Calendar c = Calendar.getInstance();
            if (!"".equals(strDate)) {
                c.setTime(fmt.parse(strDate));
            }
            c.add(Calendar.MONTH, -1);
            Date date = c.getTime();
            int day = getDay(date.getYear(), date.getMonth() - 1);
            return fmt.format(c.getTime()) + "-" + day;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    // 获取本周的第一天
    public static String getWeekData() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期
        return df.format(cal.getTime());
    }

    // 获取上周的第一天
    public static String getLastWeekData() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.WEEK_OF_MONTH, -1);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期
        return df.format(cal.getTime());
    }

    // 获取上周的最后一天
    public static String getLastWeekDataDay() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, 1);
        return df.format(cal.getTime());
    }

    // 获取昨天
    public static String getLastDay() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.add(Calendar.DATE, -1);
        return df.format(cal.getTime());
    }

}